<?php
/**
 * 工作任务指派提醒系统
 * @author C.Jango
 * 工作任务制定，指派，提醒，汇报进度等功能实现
 */
namespace Admin\Controller;

class CalendarController extends AdminController {
	function index() {
		$list = $this->lists('Calendar');
		$this->assign('_list', $list);
		$this->meta_title = '工作任务';
		$this->display();
	}
	
	function add() {
		if (IS_POST) {
			$model  = D('Calendar');
			if (!$model->create()) {
				$this->error($model->getError());
			}else {
				if ($model->add()) {
					$this->success('任务创建成功！', U('index'));
				}else {
					$this->error('任务创建失败！');
				}
			}
		}else {
			$this->meta_title = '创建任务';
			$this->display('edit');
		}
	}
	
	function edit($id) {
		$model  = D('Calendar');
		if (IS_POST) {
			if (!$model->create()) {
				$this->error($model->getError());
			}else {
				if ($model->save()) {
					$this->success('任务编辑成功！', U('index'));
				}else {
					$this->error('任务编辑失败！');
				}
			}
		}else {
			$info = $model->detail($id);
			if ($info['uid'] != UID) $this->error('无权对此任务进行编辑操作！');
			$this->assign('info', $info);
			$this->meta_title = '创建任务';
			$this->display();
		}
	}
	
	function del() {
		$id = I('id');
		empty($id) && $this->error('请选择至少一条数据！');
		$map['id'] = array('in', $id);
		$model  = D('Calendar');
		if ($this->delete($model, $map)) {
			$this->success('删除成功！');
		}else {
			$this->error('删除失败！');
		}
	}
	
	/**
	 * 分派任务
	 * @param int $id
	 */
	function distribute($id) {
		if (IS_POST) {
			$model = M('CalendarAccess');
			$cab_id = I('post.cal_id');
			foreach (I('post.to_uid') as $v) {
				$dataList[] = array('cal_id'=>$cab_id,'to_uid'=>$v,'send_time'=>NOW_TIME);
			}
			if ($model->addAll($dataList)){
				$this->success('任务分派成功！',U('index'));
			}else {
				$this->error('任务分派失败，或已分派此任务！');
			}
		}else {
			//提取文件信息
			$model = D('Calendar');
			$info = $model->field('id,title')->find($id);
			$this->assign('info',$info);
			//展示用户列表
			$gList = M('Group')->where('gid<>'.C('NEW_USER_GROUP'))->select();
			$this->assign('_glist',$gList);
			$prefix = C('DB_PREFIX');
			$userList = M('Member')->join('__CALENDAR_ACCESS__ ON __MEMBER__.uid = __CALENDAR_ACCESS__.to_uid','LEFT')->where($prefix.'calendar_access.cal_id='.$id.' AND group_id<>'.C('NEW_USER_GROUP'))->select();
			$this->assign('_user_list',$userList);
			//提取已经分发过的用户
			$recUser = M('CalendarAccess')->where('cal_id='.$id)->getField('to_uid',true);
			//将数组转换成字符串
			$recUser = arr2str($recUser);
			$this->assign('recUser',$recUser);
				
			$this->meta_title = '分派任务';
			$this->display();
		}
	}
	
	function report($cid,$uid) {
		$info = M('Calendar')->find($cid);
		$map['cal_id'] = $cid;
		$map['to_uid'] = $uid;
		$report = M('CalendarAccess')->where($map)->find();
		if ($report['return_id'] > 0) {
			$report['return_name'] = M('File')->getFieldById($report['return_id'],'name');
		}
		$this->assign('report' ,$report);
		$this->assign('info', $info);
		$this->meta_title = '查看报告';
		$this->display();
	}
	
	function mine() {
		$prefix = C('DB_PREFIX');
		$model = M('CalendarAccess')->alias('l')->join($prefix.'calendar r ON l.cal_id=r.id');
		$map['to_uid'] = UID;
		$list = $this->lists($model, $map,'send_time desc',null,'*');
		$this->assign('_list', $list);
		$this->meta_title = '我的任务';
		$this->display();
	}
	
	function over() {
		$id = I('id');
		if (empty($id)) $this->error('请选择至少一条数据！');
		$map['cal_id']      = array('in', $id);
		$map['to_uid']      = UID;
		$map['process']     = array('neq',1);
		$model = M('CalendarAccess');
		$data['finish_time'] = NOW_TIME;
		$data['process']     = 1;
		$data['remark']      = '一键完成';
		if ($model->where($map)->save($data)) {
			$this->success('任务完成成功！');
		}else {
			$this->error('任务完成失败！');
		}
	}
	
	function look($id) {
		if (IS_POST) {
			$model = M('CalendarAccess');
			$map['cal_id']  = I('post.cal_id');
			$map['to_uid']  = I('post.to_uid');
			//已经完成的任务 不允许再进行操作
			if ($model->where($map)->getField('process')==1) $this->error('已经完成的任务不能进行编辑！');
			//处理传递过来的回执文件
			$return_id = I('post.return_id');
			if (!is_numeric($return_id)) {
				$file = json_decode(think_decrypt($return_id), true);
				$return_id = !empty($file)?$file['id']:0;
			}
			
			$data['remark'] = I('post.remark');
			$data['return_id'] = $return_id;
			//保存进度，或者完成操作
			$action = I('get.action');
			$url = '';
			if ($action == 'ok') {
				$data['process'] = 1;
				$data['	finish_time'] = NOW_TIME;
				$url = U('mine');
			}
			if ($model->where($map)->save($data)) {
				$this->success('保存成功！',$url);
			}else {
				$this->error('保存失败！');
			}
		}else {
			$model = D('Calendar');
			$info = $model->calendarInfo($id);
			$this->assign('info', $info);
			$this->meta_title = '查看任务';
			$this->display();
		}
	}
	
	function download($id) {
		if (!D('File')->download(C('DOWNLOAD_UPLOAD.rootPath'), $id)) {
			$this->error('下载文件失败！');
		}
	}
}